// subPages/mine/integral/integral.js
var app = getApp();
var util = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      yz:[{
        text:'积分可通过购买商品获取'
      },{
        text:'积分可用于积分商城兑换和门店兑换服务'
      }],
      integralDetails:[],
      integral: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (!app.globalData.userInfo) {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      app.userInfoReadyCallback = res => {
        if (res) {
          that.setData({
            userInfo: res.userInfo
          });
        }
      }
    }else{
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }
  },

  //授权登录
  unLoginBtn:function(){
    var that = this
    if(app.globalData.userInfo){
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }else{
      let isLogin = app.globalData.userId?true:false 
      //未授权
      wx.navigateTo({
        url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ isLogin,
      })
    }
  },

  //获取积分
  getIntegral:function(userId){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/get_user_info',function(res){
      // console.log(res)
      if(res.data){
        that.setData({
          integral: res.data.jf
        })
      }
    },{user_id: userId})
  },

  //获取积分列表
  getIntegralList:function(userId){
    var that = this
    util.getAjax(`${app.globalData.apiUrl}/user_get_jf_list`,function(res){
      // console.log(res)
      if(res.data.length != 0){
        for(var i in res.data){
          if(res.data[i].time){
            res.data[i].time = util.setTime2(res.data[i].time)
          }
        }
        that.setData({
          integralDetails: res.data,
          isIntegral: true,
        })
      }
    },{user_id: userId})
  },

  ordersBtn:function(){
    wx.navigateTo({
      url: '/subPages/integral/orders/orders'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getIntegral(that.data.userId)
      that.getIntegralList(that.data.userId)
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getIntegral(that.data.userId)
          that.getIntegralList(that.data.userId)
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
